package com.cl.ks.controller;

import com.cl.ks.service.KsTableService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
/**
 * @author chenyitao
 * @date 2021/5/6
 */
@RestController
@RequestMapping("/table")
@CrossOrigin
public class KsTableController {

    private final KsTableService ksTableService;

    public KsTableController(KsTableService ksTableService) {
        this.ksTableService = ksTableService;
    }

    @RequestMapping(path = "/init", method = RequestMethod.POST)
    public ResponseEntity<Object> initTableMapping(@RequestBody KsTableService.Table table) {
        return ksTableService.initTableMapping(table);
    }

    @RequestMapping(path = "/original/list", method = RequestMethod.GET)
    public ResponseEntity<Object> getOriginalTables() {
        return ksTableService.getOriginalTables();
    }

    @RequestMapping(path = "/mapping/list", method = RequestMethod.POST)
    public ResponseEntity<Object> getMappingTables(@RequestBody Map<String, Object> param) {
        return ksTableService.getMappingTables(param);
    }

    @RequestMapping(path = "/mapping/delete", method = RequestMethod.GET)
    public ResponseEntity<Object> deleteMappingTable(@RequestParam(name = "targetTable") String targetTable) {
        return ksTableService.deleteMappingTable(targetTable);
    }

    @RequestMapping(path = "/original/fields", method = RequestMethod.GET)
    public ResponseEntity<Object> getOriginalFields(@RequestParam(name = "targetTable") String targetTable) {
        return ksTableService.getOriginalFields(targetTable);
    }

    @RequestMapping(path = "/mapping/fields", method = RequestMethod.GET)
    public ResponseEntity<Object> getMappingFields(@RequestParam(name = "targetTable") String targetTable,@RequestParam(name = "dbCode",defaultValue = "") String dbCode) {
        return ksTableService.getMappingFields(targetTable,dbCode);
    }

    @RequestMapping(path = "/join", method = RequestMethod.GET)
    public ResponseEntity<Object> getJoinTables(@RequestParam(name = "targetTable") String targetTable) {
        return ksTableService.getJoinTables(targetTable);
    }

    @RequestMapping(path = "/rules", method = RequestMethod.GET)
    public ResponseEntity<Object> rlKsTableRules(@RequestParam(name = "tableName") String tableName) {
        return ksTableService.rlKsTableRules(tableName);
    }
}
